package com.soriya.nestlive.util.result;

import lombok.Data;

@Data
public class Result<T> {
// 统一返回结果

    // 状态码
    private Integer code;
    // 消息
    private String message;
    // 返回内容
    private T data;

    public Integer getCode() {
        return code;
    }

    public Result<?> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result<?> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<?> setData(T data) {
        this.data = data;
        return this;
    }

    // 返回无数据的成功结果
    public static <T> Result<T> success() {
        return success(null);
    }

    // 返回有数据的成功结果
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        if (data != null) result.setData(data);
        return result;
    }

    // 返回无数据的失败结果
    public static <T> Result<T> failed() {
        return failed(null);
    }

    // 返回有数据的失败结果
    public static <T> Result<T> failed(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.FAIL.getCode());
        result.setMessage(ResultCode.FAIL.getMessage());
        if (data != null) result.setData(data);
        return result;
    }

    public static Result<?> update(boolean isSuccess) {
        return isSuccess ? Result.success() : Result.failed();
    }
}
